home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 351-375 / disk_362 / puzz / source / loadpuzzle.c < prev    next >
C/C++ Source or Header  |  1992-05-06  |  5KB  |  232 lines

  1. /*    Reads arguments from CLI or Workbench, and loads the specified puzzle.
  2.     
  3.     Martin J. Round January 1990
  4. */
  5.  
  6. char line[MAXLINE];
  7.  
  8. extern struct WBStartup *WBenchMsg;
  9.  
  10. extern void message (UBYTE *);
  11. extern void print (char *);
  12. extern struct Screen *ReadILBM(BPTR);
  13.  
  14. extern int runningfromcli;
  15.  
  16. extern struct IntuiText instrtext[6];
  17. extern int sqrsacross,sqrsdown;
  18. extern unsigned char start [MAXACROSS] [MAXDOWN];
  19. extern unsigned char goal  [MAXACROSS] [MAXDOWN];
  20. extern int topleftx,toplefty;
  21. extern int sqrwidth,sqrheight;
  22. extern int slide;
  23. extern int scorex,scorey,scoreapen,scorebpen;
  24.  
  25. int readline(BPTR fp, char *p, int max)    /*    returns # chars. -1 = fail    */
  26.     {
  27.     int i = 0;
  28.     char c;
  29.  
  30.     --max;
  31.     
  32.     if (Read(fp,&c,1) <=0)
  33.         return (-1);
  34.  
  35.     while ((c != '\n') && (i < max))
  36.         {
  37.         *p++ = c;
  38.         i++;
  39.         if (Read(fp,&c,1) <=0)
  40.             return (-1);
  41.         }
  42.     
  43.     *p = 0;
  44.     
  45.     return(i);
  46.     }
  47.     
  48. int value(char *p)
  49.     {    
  50.     return (atoi(strtok(stpblk(p)," ;")));
  51.     }
  52.  
  53. int readfi(BPTR fp)    /*    1 = not a puzz, 2 = wrong ver. 3 = bad    */
  54.     {
  55.     int i,j;
  56.     char *p;
  57.     
  58.     if ((i = readline(fp,line,MAXLINE)) < 8) return (1);
  59.     
  60.     if (strncmp(line,"PUZZ ",5)) return (1);
  61.     
  62.     if (strncmp(line,"PUZZ 1.0",8)) return (2);
  63.     
  64.     for (i=0; i<6; i++)
  65.         {
  66.         if (readline(fp,line,MAXLINE) < 0) return(3);
  67.         strncpy((instrtext[i]).IText,line,26);
  68.         }
  69.     
  70.     if (readline(fp,line,MAXLINE) < 0) return(3);
  71.     sqrsacross = value(line);
  72.  
  73.     if (readline(fp,line,MAXLINE) < 0) return(3);
  74.     sqrsdown = value(line);
  75.     
  76.     for (j=0; j<sqrsdown; j++)
  77.         {
  78.         if (readline(fp,line,MAXLINE) < 0) return(3);
  79.         p = strtok(stpblk(line)," ,");
  80.         start [0] [j] = (unsigned char) atoi(p);
  81.         for (i=1; i<sqrsacross; i++)
  82.             {
  83.             if (i < (sqrsacross-1))
  84.                 p = strtok(NULL," ,");
  85.             else
  86.                 p = strtok(NULL," ;");
  87.             start [i] [j] = (unsigned char) atoi(p);
  88.             }
  89.         }
  90.  
  91.     for (j=0; j<sqrsdown; j++)
  92.         {
  93.         if (readline(fp,line,MAXLINE) < 0) return(3);
  94.         p = strtok(stpblk(line)," ,");
  95.         goal [0] [j] = (unsigned char) atoi(p);
  96.         for (i=1; i<sqrsacross; i++)
  97.             {
  98.             if (i < (sqrsacross-1))
  99.                 p = strtok(NULL," ,");
  100.             else
  101.                 p = strtok(NULL," ;");
  102.             goal [i] [j] = (unsigned char) atoi(p);
  103.             }
  104.         }
  105.  
  106.     if (readline(fp,line,MAXLINE) < 0) return(3);
  107.     topleftx = value(line);
  108.  
  109.     if (readline(fp,line,MAXLINE) < 0) return(3);
  110.     toplefty = value(line);
  111.  
  112.     if (readline(fp,line,MAXLINE) < 0) return(3);
  113.     sqrwidth = value(line);
  114.  
  115.     if (readline(fp,line,MAXLINE) < 0) return(3);
  116.     sqrheight = value(line);
  117.  
  118.     if (readline(fp,line,MAXLINE) < 0) return(3);
  119.     slide = value(line);
  120.     
  121.     if (readline(fp,line,MAXLINE) < 0) return(3);
  122.     scorex = value(line);
  123.     
  124.     if (readline(fp,line,MAXLINE) < 0) return(3);
  125.     scorey = value(line);
  126.     
  127.     if (readline(fp,line,MAXLINE) < 0) return(3);
  128.     scoreapen = value(line);
  129.     
  130.     if (readline(fp,line,MAXLINE) < 0) return(3);
  131.     scorebpen = value(line);
  132.     
  133.     do
  134.         {
  135.         if (readline(fp,line,MAXLINE) < 0) return(3);
  136.         }    while (strncmp(line,"**ILBM**",8));
  137.  
  138.     return(0);
  139.     }
  140.  
  141. struct Screen *readfile(BPTR fp,char *file)
  142.     {
  143.     struct Screen *screen;
  144.     
  145.     switch (readfi(fp))
  146.         {
  147.         case 1:
  148.             sprintf(line,"%s is not a puzzle file.",file);
  149.             message(line);
  150.             Close(fp);
  151.             return(NULL);
  152.             break;
  153.         case 2:
  154.             sprintf(&(line[8])," or later is required.");
  155.             message(line);
  156.             Close(fp);
  157.             return(NULL);
  158.             break;
  159.         case 3:
  160.             sprintf(line,"%s has wrong format.",file);
  161.             message(line);
  162.             Close(fp);
  163.             return(NULL);
  164.             break;
  165.         }
  166.     if ((screen = ReadILBM(fp)) == NULL)
  167.         message("Bad or Missing ILBM section.");
  168.     Close(fp);
  169.     return(screen);
  170.     }
  171.  
  172. struct Screen *loadpuzzle (int argc,char **argv)    /*    NULL = failed    */
  173.     {
  174.     BPTR fp,olddir;
  175.     struct Screen *screen;
  176.     
  177.     /*    Check we've got something to do    */
  178.     if (runningfromcli && argc == 1)
  179.         {
  180.         print("Useage:      PUZZ puzzlename\n\n");
  181.         print("For example: PUZZ 15.puzzle\n\n");
  182.         return(NULL);
  183.         }
  184.  
  185.     if (!runningfromcli && ((WBenchMsg==NULL) || (WBenchMsg->sm_NumArgs < 2)))
  186.             {
  187.             message ("You must click on a puzzle icon - try '15.puzzle'.");
  188.             return(NULL);
  189.             }
  190.  
  191.     if (runningfromcli)
  192.         {
  193.         if ((fp=Open(argv[1],MODE_OLDFILE))==NULL)
  194.             {
  195.             sprintf(line,"Can't open file: %s\n",argv[1]);
  196.             print(line);
  197.             return(NULL);
  198.             }
  199.         else
  200.             {
  201.             screen = readfile(fp,argv[1]);
  202.             }
  203.  
  204.         }
  205.     else
  206.         {    /*    running from Workbench    */
  207.         olddir = CurrentDir (WBenchMsg->sm_ArgList[1].wa_Lock);
  208.         if
  209.             (
  210.                 (fp = Open(WBenchMsg->sm_ArgList[1].wa_Name,MODE_OLDFILE))
  211.                 == NULL
  212.             )
  213.             {
  214.             sprintf
  215.                 (
  216.                 line,
  217.                 "Can't open file: %s",
  218.                 WBenchMsg->sm_ArgList[1].wa_Name
  219.                 );
  220.                 
  221.             message(line);
  222.             if (olddir) CurrentDir(olddir);
  223.             return(NULL);
  224.             }
  225.         else
  226.             {
  227.             screen = readfile(fp,WBenchMsg->sm_ArgList[1].wa_Name);
  228.             }
  229.         if (olddir) CurrentDir(olddir);
  230.         }
  231.     return (screen);
  232.     }